﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class SpriteAnimator : MonoBehaviour
{
    [CompilerGenerated]
    private static Func<Animation, string> <>f__am$cache6;
    [CompilerGenerated]
    private static Func<Animation, Animation> <>f__am$cache7;
    [SerializeField]
    private List<Animation> animations = new List<Animation>();
    private Dictionary<string, Animation> lookup = new Dictionary<string, Animation>();
    private SpriteRenderer m_sr;
    private Coroutine play;
    public bool playOnEnable = true;

    public bool ContainsAnimation(string name)
    {
        return this.lookup.ContainsKey(name);
    }

    public Coroutine ForcePlay(Animation anim)
    {
        if (this.play != null)
        {
            base.StopCoroutine(this.play);
        }
        this.play = base.StartCoroutine(this.Play(anim));
        return this.play;
    }

    public Coroutine ForcePlay(string name)
    {
        Animation anim = this.GetAnimation(name);
        if (anim == null)
        {
            return null;
        }
        return this.ForcePlay(anim);
    }

    public Animation GetAnimation(string name)
    {
        if (!this.ContainsAnimation(name))
        {
            object[] args = new object[] { name };
            UnityEngine.Debug.LogWarningFormat("Could not find animation by name '{0}'", args);
            return null;
        }
        return this.lookup[name];
    }

    public List<Animation> GetAnimations()
    {
        return this.animations;
    }

    public bool IsPlaying(string name)
    {
        return (this.curPlayingAnimationName == name);
    }

    private void OnEnable()
    {
        if (this.playOnEnable)
        {
            this.ForcePlay(this.animations.FirstOrDefault<Animation>());
        }
    }

    [DebuggerHidden]
    private IEnumerator Play(Animation anim)
    {
        return new <Play>c__Iterator2B { anim = anim, <$>anim = anim, <>f__this = this };
    }

    public Coroutine Play(string name)
    {
        if (this.curPlayingAnimationName == name)
        {
            return null;
        }
        if (!this.ContainsAnimation(name))
        {
            return null;
        }
        return this.ForcePlay(name);
    }

    public void SetAnimations(List<Animation> animations)
    {
        this.animations = animations;
        if (<>f__am$cache6 == null)
        {
            <>f__am$cache6 = x => x.name;
        }
        if (<>f__am$cache7 == null)
        {
            <>f__am$cache7 = x => x;
        }
        this.lookup = animations.ToDictionary<Animation, string, Animation>(<>f__am$cache6, <>f__am$cache7, StringComparer.CurrentCultureIgnoreCase);
    }

    public Animation curPlayingAnimation { get; private set; }

    public string curPlayingAnimationName
    {
        get
        {
            return ((this.curPlayingAnimation == null) ? null : this.curPlayingAnimation.name);
        }
    }

    public bool isPlaying
    {
        get
        {
            return (this.curPlayingAnimation != null);
        }
    }

    public SpriteRenderer sr
    {
        get
        {
            return ((this.m_sr == null) ? (this.m_sr = base.GetComponent<SpriteRenderer>()) : this.m_sr);
        }
    }

    [CompilerGenerated]
    private sealed class <Play>c__Iterator2B : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        internal SpriteAnimator.Animation <$>anim;
        internal SpriteAnimator <>f__this;
        internal SpriteAnimator.Frame <frame>__1;
        internal int <i>__0;
        internal SpriteAnimator.Animation anim;

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    if (this.anim != null)
                    {
                        this.<>f__this.curPlayingAnimation = this.anim;
                        this.<i>__0 = 0;
                        while (this.<i>__0 < this.anim.frames.Count)
                        {
                            this.<frame>__1 = this.anim.frames[this.<i>__0];
                            this.<>f__this.sr.sprite = this.<frame>__1.sprite;
                            this.$current = new WaitForSeconds(this.<frame>__1.duration);
                            this.$PC = 1;
                            return true;
                        Label_00A7:
                            this.<i>__0++;
                        }
                        if (this.anim.loop)
                        {
                            this.<>f__this.ForcePlay(this.anim);
                        }
                        else
                        {
                            this.$PC = -1;
                        }
                        break;
                    }
                    break;

                case 1:
                    goto Label_00A7;
            }
            return false;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }

    [Serializable]
    public class Animation
    {
        public List<SpriteAnimator.Frame> frames = new List<SpriteAnimator.Frame>();
        public bool loop = true;
        public string name = "Default";
    }

    [Serializable]
    public class Frame
    {
        public float duration = 0.1f;
        public Sprite sprite;
    }
}

